package com.soilflow.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.annotation.DataScope;
import com.soilflow.common.domain.LandLaw;
import com.soilflow.common.domain.LandNews;
import com.soilflow.common.domain.dto.LandLawDto;
import com.soilflow.common.domain.dto.LandNewsDto;
import com.soilflow.common.domain.vo.LandLawVo;
import com.soilflow.common.domain.vo.LandNewsVo;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.info.mapper.LandLawMapper;
import com.soilflow.info.service.LandLawService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class LandLawServiceImpl extends ServiceImpl<LandLawMapper, LandLaw> implements LandLawService {

    @Resource
    private LandLawMapper landLawMapper;


    @DataScope(deptAlias = "ll")
    @Override
    public List<LandLawVo> landLawList(LandLawDto dto) {
        return landLawMapper.landLawList(dto);
    }

    @Override
    public Boolean addLawNotice(LandLawDto dto) {
        //1 查询新闻信息
        LambdaQueryWrapper<LandLaw> query = new LambdaQueryWrapper<>();
        query.eq(StringUtils.isNotEmpty(dto.getLawTitle()),LandLaw::getLawTitle,dto.getLawTitle()).last("limit 1");

        if (ObjectUtils.isEmpty(landLawMapper.selectOne(query))){
            LandLaw landLaw = new LandLaw();
            BeanUtils.copyProperties(dto,landLaw);
            landLawMapper.insert(landLaw);
            return true;
        }

        return false;
    }

    @Override
    public void updateLandLaw(LandLawDto dto) {
        LandLaw landLaw = new LandLaw();
        BeanUtils.copyProperties(dto,landLaw);
        landLaw.setId(dto.getId());
        landLawMapper.updateById(landLaw);
    }

    @Override
    public LandLawVo getLandLawById(Long id) {
        LandLawVo landLawVo = new LandLawVo();
        LandLaw landLaw = landLawMapper.selectById(id);
        BeanUtils.copyProperties(landLaw,landLawVo);
        //格式化新闻日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String setOutTime = sdf.format(landLaw.getCreateTime());
        landLawVo.setSetOutTime(setOutTime);
        return landLawVo;
    }


    /**
     * 前台法律列表
     * @param dto
     * @return
     */
    @Override
    public List<LandLawVo> lawList(LandLawDto dto) {
        return landLawMapper.landLawList(dto);
    }
}
